package cn.nuc.edu.onlinestore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.nuc.edu.onlinestore.entity.SysFunction;
import cn.nuc.edu.onlinestore.service.interfaces.IFunService;
import cn.nuc.edu.onlinestore.util.PageControl;
import cn.nuc.edu.onlinestore.util.Pager;

/**
 * 功能管理handler
 * @author 王凯
 *
 */
@Controller
@RequestMapping("/function")
public class FunHandler {

	@Autowired
	private IFunService service;
	
	/**
	 * 初始化添加功能页面
	 * @param pid 父功能id，非必须
	 * @param pname 父功能名称，非必须
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "/initadd/{pid}/{pname}", method = {RequestMethod.GET})
	public ModelAndView initadd(
			@PathVariable(value = "pid", required = false) Integer pid,
			@PathVariable(value = "pname", required = false) String pname,
			ModelAndView modelAndView) {
		modelAndView.addObject("pid", pid);
		modelAndView.addObject("pname", pname);
		modelAndView.setViewName("forward:/WEB-INF/views/fun/add.jsp");
		return modelAndView;
	}
	
	/**
	 * 修改系统功能
	 */
	@RequestMapping(value="/edit", method=RequestMethod.PUT)
	public ModelAndView edit(SysFunction function, ModelAndView modelAndView) {
		Integer rtn = service.edit(function);
		if (rtn > 0) {
			modelAndView.setViewName("redirect:/function/funs");
		} else {
			modelAndView.addObject("msg", "保存系统功能失败！");
			modelAndView.setViewName("forward:/WEB-INF/views/fun/edit.jsp");
		}
		return modelAndView;
	}

	/**
	 * 初始化修改页面
	 */
	@RequestMapping(value="/toedit/{id}", method=RequestMethod.GET)
	public ModelAndView toedit(@PathVariable("id") Integer id, ModelAndView modelAndView){

		SysFunction fun = new SysFunction();
		fun.setFunid(id);
		fun = service.detail(fun);
		if (fun != null) {
			modelAndView.addObject("item", fun);
			modelAndView.setViewName("forward:/WEB-INF/views/fun/edit.jsp");
		} else {
			modelAndView.addObject("msg", "需要修改的系统功能不存在。");
			modelAndView.setViewName("redirect:funs");
		}
		return modelAndView;
	}

	/**
	 * 查询功能列表
	 * 
	 */
	@RequestMapping("/funs{index}")
	public ModelAndView funlist(String sname, 
				@PathVariable(value = "index", required = false) Integer index,
				ModelAndView modelAndView) {

		SysFunction fun = new SysFunction();
		if (sname != null && !sname.equals("")) {
			fun.setFunname("%" + sname + "%");
		}

		PageControl pc = new PageControl();
		if (index == null) {
			index = 1;
		}
		pc.setCurrentindex(index);
		pc.setPagesize(5);

		Pager<SysFunction> pager = service.list(fun, pc);
		System.out.println(pager.getList());
		modelAndView.addObject("pager", pager);
		modelAndView.setViewName("forward:/WEB-INF/views/fun/list.jsp");
		return modelAndView;
	}

	/**
	 * 增加系统功能
	 * 
	 */
	@RequestMapping(value = "/addfun", method={RequestMethod.POST})
	public ModelAndView addfun(SysFunction function, ModelAndView modelAndView) {

		Integer rtn = service.addfun(function);
		if (rtn > 0) {
			modelAndView.setViewName("redirect:/function/funs");
		} else {
			modelAndView.addObject("msg", "保存系统功能失败！");
			modelAndView.setViewName("forward:/WEB-INF/views/fun/add.jsp");
		}
		return modelAndView;
	}
}
